package data;

import java.io.*;
import java.util.StringTokenizer;

public class RuleSet {

	public Rule[] rules;
	public int num_rules=0;

	public RuleSet(String filename){
		try{
			File file = new File(filename);
			if (file == null || !file.exists ()) {
				System.out.println ("Config file could not be opened");
			}
			else{
				FileReader file_reader = new FileReader (file);
				BufferedReader buf_reader = new BufferedReader (file_reader);
				do {
					String line = buf_reader.readLine ();
					if (line == null) break;
					if (parseLine(line)) num_rules++;
				} while (true);
				buf_reader.close ();
			}
			rules= new Rule[num_rules];
			addRules(filename);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	public boolean parseLine(String line){
		String check[] = new String[4];
		check = line.split(" ");
		if(check.length<=2 || check.length >4){
			return false;
		}
		if(check[0].equals("allow") || check[0].equals("block")){
			int port =0;
			if(!check[2].equals("*")){
				try{
					port = Integer.parseInt(check[2]);
				}
				catch(Exception e){
					return false;
				}
			}
			if((port > 0 && port < 65536) || check[2].equals("*")){
				if(isValidIP(check[1])){   //XXXXX- IP prefix checking
					if(check.length == 3){
						return true;
					}
					else{
						if(check[3]==null || check[3].equals("established")){
							return true;
						}
						else return false;
					}
				}
				else return false;
			}
			else return false;
		}
		else return false;
	}


	private boolean isValidIP(String ip) {
		if(!ip.equals("*")){
			if(ip.contains("/")){
				int endIndex=ip.indexOf("/");
				String ipp=ip.substring(0, endIndex);
				StringTokenizer st = new StringTokenizer(ipp, ".");
				//save prefix
				if(st.countTokens()!=4){
					return false;
				}
				String prefix=ip.substring(endIndex+1,ip.length());
				//System.out.println(prefix);
				//System.out.println(ipp);
				try{
					int te=Integer.parseInt(prefix);
					if(te < 0 || te > 32){
						return false;
					}
				}
				catch(Exception e){
					return false;
				}
				while (st.hasMoreTokens()) {
					try{
						//st.nextToken().getBytes();
						int tempi= Integer.parseInt((st.nextToken()));
						if(tempi<0 || tempi>255){
							return false;
						}
					}catch(Exception e){
						return false;
					}
				}
			}
			else{
				StringTokenizer st = new StringTokenizer(ip, ".");

				while (st.hasMoreTokens()) {
					try{
						//st.nextToken().getBytes();
						int tempi= Integer.parseInt((st.nextToken()));
						if(tempi<0 || tempi>255){
							return false;
						}
					}
					catch(Exception e){
						return false;
					}
				}
			}
			return true;
		}
		else{
			return true;
		}
	}


	private void addRules(String filename){
		int k =0;
		try{
			File file = new File(filename);
			if (file == null || !file.exists ()) {
				System.out.println ("Config file could not be opened");
			}
			else{
				FileReader file_reader = new FileReader (file);
				BufferedReader buf_reader = new BufferedReader (file_reader);
				do {
					String line = buf_reader.readLine ();
					if (line == null) break;
					if (parseLine(line)){
						Rule r = new Rule(line);
						rules[k]=r;
						k++;
					}
				} while (true);
				buf_reader.close ();
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}


	public void printRules(){
		int i,j;
		for(i=0;i<rules.length;i++){
			System.out.print(i+" ");
			for(j=0;j<rules[i].rule.length;j++){
				System.out.print("\t"+rules[i].rule[j]);
			}
			System.out.println();
		}
	}

}

